using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for FormsControl.
	/// </summary>
	public class FormsControl : AdminUserControl
	{
    private ExtendedDataGrid formsGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public FormsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actionList.Add("Open selected forms", "Open the selected forms", new ActionExecuteDelegate(OpenForms));
      actionList.Add("Copy to clipboard", "Copy the list to the clipboard", new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.formsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.formsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // formsGrid
      // 
      this.formsGrid.AllowNavigation = false;
      this.formsGrid.CaptionVisible = false;
      this.formsGrid.DataMember = "";
      this.formsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.formsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.formsGrid.Location = new System.Drawing.Point(0, 0);
      this.formsGrid.Name = "formsGrid";
      this.formsGrid.Size = new System.Drawing.Size(314, 368);
      this.formsGrid.TabIndex = 0;
      this.formsGrid.OpenSelected += new System.EventHandler(this.formsGrid_OpenSelected);
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(314, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 368);
      this.uiActionsControl.TabIndex = 1;
      // 
      // FormsControl
      // 
      this.Controls.Add(this.formsGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "FormsControl";
      this.Size = new System.Drawing.Size(464, 368);
      ((System.ComponentModel.ISupportInitialize)(this.formsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private FreeFlow.Administration.FormCollection forms;
    public void LoadForms(FreeFlow.Administration.FormCollection forms)
    {
      this.forms = forms;
      formsGrid.DataSource = null;
      formsGrid.DataSource = forms;
      formsGrid.ClearColumns();
      formsGrid.AddColumn("Name");
      formsGrid.AddColumn("RestrictViewingTo", "Restrict viewing to");
      formsGrid.AddColumn("WhenUserLoadsForm", "When user loads form");
      formsGrid.AddColumn("WhenUserSavesForm", "When user saves form");
      formsGrid.AutosizeColumns();
    }

    private string RefreshList()
    {
      forms.Refresh();
      LoadForms(forms);
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      formsGrid.CopyToClipboard();
      return null;
    }

    private string OpenForms()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)formsGrid.BindingContext[formsGrid.DataSource];
        for (int i = 0; i < bm.Count; i++)
        {
          if (formsGrid.IsSelected(i))
          {
            FreeFlow.Administration.Form form = (FreeFlow.Administration.Form)bm.List[i];
            FormForm formForm = new FormForm();
            formForm.LoadForm(form);
            formForm.Show();
          }
        }
      }
      return null;
    }

    private void formsGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenForms();
    }
	}
}
